package com.luych.cloud.database.comparetor.mysql;

import com.luych.cloud.database.comparetor.core.Column;
import com.luych.cloud.database.comparetor.core.Table;
import org.springframework.util.StringUtils;

import java.util.List;

public class MysqlTable implements Table {

    private String name;
    private String comment;
    private List<Column> columns;
    private List<String> primaryKey;

    public MysqlTable() {
    }

    public MysqlTable(String name, String comment, List<Column> columns, List<String> primaryKey) {
        this.name = name;
        this.comment = comment;
        this.columns = columns;
        this.primaryKey = primaryKey;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public void setColumns(List<Column> columns) {
        this.columns = columns;
    }

    public void setPrimaryKey(List<String> primaryKey) {
        this.primaryKey = primaryKey;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getComment() {
        return comment;
    }

    @Override
    public List<Column> getColumns() {
        return columns;
    }

    @Override
    public List<String> getPrimaryKey() {
        return primaryKey;
    }

    @Override
    public String getCreateVolumeSql(String tableName, Column column) {
        String sql = "ALTER TABLE " + tableName.toLowerCase() + " ADD COLUMN " + column.getName().toLowerCase() + " " + column.getColumnType();
        if (!column.isNullable()) {
            sql += " NOT NULL";
        }
        if (!StringUtils.isEmpty(column.getComment())) {
            sql += " COMMENT '" + column.getComment() + "'";
        }
        sql += ";";
        return sql;
    }

    @Override
    public String getModifyVolumeSql(String tableName, Column column) {
        String sql = "ALTER TABLE " + tableName.toLowerCase() + " MODIFY COLUMN " + column.getName().toLowerCase() + " " + column.getColumnType();
        if (!column.isNullable()) {
            sql += " NOT NULL";
        }
        if (!StringUtils.isEmpty(column.getComment())) {
            sql += " COMMENT '" + column.getComment() + "'";
        }
        sql += ";";
        return sql;
    }

    @Override
    public String getRemoveVolumeSql(String tableName, String columeName) {
        return "ALTER TABLE " + tableName.toLowerCase() + " DROP COLUMN " + columeName.toLowerCase() + ";";
    }
}
